package ebcs.scan.controller.reviewCtr;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;

import ebcs.database.bean.BsGroupInfor;
import ebcs.database.bean.BsNeedInfor;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.bean.BsUserNeedRel;
import ebcs.database.bean.PaUserRole;
import ebcs.database.bean.RpUserReportBean;
import ebcs.database.service.BsUserInforServiceImp;
import ebcs.database.service.BsUserNeedServiceImp;
import ebcs.scan.common.consts.KLinkUsSysConsts;
import ebcs.scan.common.utils.HttpClientUtil;
import ebcs.scan.common.utils.StringUtil;
import ebcs.scan.controller.batch.BatchManagerCtrl;
import ebcs.scan.controller.systemCtr.SystemManagerCtrl;
import ebcs.scan.controller.userCtr.UserManagerCtrl;
import ebcs.scan.service.wechat.WechatCtrlService;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.config.JAppConfig;
import jplat.tools.string.DateUtil;

/**
 * 报工审核
 * @author jinmuyong
 *
 */
@Controller
public class ReviewJobCtrl extends JAppBaseService
{
	private Logger logger = Logger.getLogger(ReviewJobCtrl.class);
	
	@Autowired 
	public BsUserInforServiceImp userInforService ; 
	
	@Autowired 
	public BatchManagerCtrl batchManagerCtrl ; 
	
	@Autowired 
	public SystemManagerCtrl systemManagerCtrl ; 
	
	@Autowired
	private BsUserNeedServiceImp userNeedService;
	
	@Autowired
    public WechatCtrlService wechatService;
	
	@RequestMapping("/page/job/reviewJob.do")
	public String reviewJob( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
 		BsUserInfor user = context.getUserInfo();
 		String sysDate = DateUtil.getSystemTimeBySomeFormat("yyyyMMdd"); 
 		
 		BsUserInfor user1 = userInforService.selectUserInforByUserNo(user.getRouid());
 		
 		// 先判断登录的是团队经理还是项目经理
 		if("69".equals(user1.getPosition_type())) {
 			
 			// 如果登录的是团队经理，则查询该团队下的所有项目经理的报工情况
 			user1.setProGroupNo(user1.getProGroupNo());
 			List<BsUserInfor> bsUserInfor = userInforService.selectTeamManager(user1);
 			if(bsUserInfor.size()==0) {
 				// 如果该团队下没有项目经理,则查询所有人的报工情况
 				RpUserReportBean record = new RpUserReportBean();
 	 	 		record.setRouid(user1.getRouid());
 	 	 		record.setProTeamNo(user1.getProTeamNo());
 	 	 		// 该项目经理下的所有人员的报工情况
 	 	 		List<RpUserReportBean> rsList = userNeedService.searchReviewJob(record);
 	 	 		
 	 	 		BsNeedInfor needInfor = new BsNeedInfor();
 	 	 		needInfor.setProTeamNo(user1.getProTeamNo());
 	 	 		needInfor.setRouid(user1.getRouid());
 	 	 		// 查询该项目经理下所有人员未审核和审核未通过的需求信息
 	 	 		//查询该项目经理项目组下所有需求信息
 	 	 		List<BsNeedInfor> needInforList = userNeedService.searchNeedInforList(needInfor);
 	 	 		/*JSONArray json = new JSONArray();
	 	         for(BsNeedInfor pLog : needInforList){
	 	             JSONObject jo = new JSONObject();
	 	             jo.put("rouid", pLog.getRouid());
	 	             jo.put("needNo", pLog.getNeedNo());
	 	             jo.put("needName", pLog.getNeedName());
	 	             json.add(jo);
	 	         }*/
 	 	         
 	 	 		request.setAttribute("needInforList", needInforList);
 	 	 		request.setAttribute("list", rsList);
 			}else {
 				RpUserReportBean record = new RpUserReportBean();
 	 	 		record.setRouid(user1.getRouid());
 	 	 		//record.setProGroupNo(user1.getProGroupNo());
 	 	 		record.setProTeamNo(user1.getProTeamNo());
 	 	 		// 如果有项目经理,则查询该团队经理团队下的所有项目经理的报工情况
 	 	 		List<RpUserReportBean> rsList = userNeedService.searchTeamReviewJob(record);	
 	 	 		
 	 	 		BsNeedInfor needInfor = new BsNeedInfor();
 	 	 		//needInfor.setProGroupNo(user1.getProGroupNo());
 	 	 		needInfor.setProTeamNo(user1.getProTeamNo());
 	 	 		needInfor.setRouid(user1.getRouid());
 	 	 		// 查询出该团队下的所有项目经理未审核和审核未通过的需求信息
 	 	 		List<BsNeedInfor> needInforList = userNeedService.searchTeamNeedInforList(needInfor);	
 	 	 		/*JSONArray json = new JSONArray();
	 	         for(BsNeedInfor pLog : needInforList){
	 	             JSONObject jo = new JSONObject();
	 	             jo.put("rouid", pLog.getRouid());
	 	             jo.put("needNo", pLog.getNeedNo());
	 	             jo.put("needName", pLog.getNeedName());
	 	             json.add(jo);
	 	         }*/
	 	         
	 	 		request.setAttribute("needInforList", needInforList);
 	 	 		request.setAttribute("list", rsList);
 			}
 		}else if("70".equals(user1.getPosition_type())) {
 			// 如果登录的是项目经理，则查询该项目组下的所有人员报工情况
 			
 			RpUserReportBean record = new RpUserReportBean();
 	 		record.setRouid(user1.getRouid());
 	 		record.setProTeamNo(user1.getProTeamNo());
 	 		// 该项目经理下的所有人员的报工情况
 	 		List<RpUserReportBean> rsList = userNeedService.searchReviewJob(record);
 	 		
 	 		BsNeedInfor needInfor = new BsNeedInfor();
 	 		needInfor.setProTeamNo(user1.getProTeamNo());
 	 		needInfor.setRouid(user1.getRouid());
 	 		// 查询该项目经理下所有人员未审核和审核未通过的需求信息
 	 		List<BsNeedInfor> needInforList = userNeedService.searchNeedInforList(needInfor);
 	 		/*JSONArray json = new JSONArray();
	         for(BsNeedInfor pLog : needInforList){
	             JSONObject jo = new JSONObject();
	             jo.put("rouid", pLog.getRouid());
	             jo.put("needNo", pLog.getNeedNo());
	             jo.put("needName", pLog.getNeedName());
	             json.add(jo);
	         }*/
 	         
 	 		request.setAttribute("needInforList", needInforList);
 	 		request.setAttribute("list", rsList);
 		}
 		request.setAttribute("wechatUrl", "0");
 		request.setAttribute("positionType", user1.getPosition_type());
 		request.setAttribute("proTeamNo", user1.getProTeamNo());
 		request.setAttribute("proGroupNo", user1.getProGroupNo());
 		request.setAttribute("rouid", user1.getRouid());
 		request.setAttribute("happenDate", "");
 		request.setAttribute("needId", "");
 		//request.setAttribute("choseName", "");
 		return "frame/html/job/reviewJob";
	}
	
	/**
	 * 查询项目经理管理下所有人员的未审核/审核未通过的报工记录
	 */
	@RequestMapping("/page/job/searchReviewJob1.do")
	public String searchReviewJob1( HttpServletRequest request,HttpServletResponse response, String happenDate) throws JTransException, JSystemException{
		String code = request.getParameter("code");
        if (StringUtil.isNotEmpty(code)) {
            logger.info("wechat----->code:" + code);
            String openid = "";
            String wechatBackJson = HttpClientUtil.htmlstr(wechatService.getUserInfoByCode(code));
            logger.info("wechatBackJson:" + wechatBackJson);
            Gson gson = new Gson();
            Map tokenGsonMap = gson.fromJson(wechatBackJson, HashMap.class);
            openid = String.valueOf(tokenGsonMap.get("openid"));
            
            logger.info("loginWechat:" + openid);
            request.setAttribute("openid", openid);
            request.setAttribute("wechatUrl", "0");
            
            BsUserInfor user1 = userInforService.selectUserInforByOpenid(openid);
            String positionType = "";
            String proGroupNo = "";
            String rouid = "";
            String proTeamNo = "";
            if(user1 != null) {
            	positionType = user1.getPosition_type();
                proGroupNo = user1.getProGroupNo();
                rouid = user1.getRouid();
                proTeamNo = user1.getProTeamNo();
                
                user1.setLoginType(KLinkUsSysConsts.USER_LOGIN_TYPE_WECHAT);
                systemManagerCtrl.saveSessionInfor(request, response, user1);	// 保存用户信息至session
                
                logger.info("userInfor+++++++++++++++++++++++++++++:" + positionType+proGroupNo+proTeamNo+happenDate);
        		if("69".equals(positionType)) {
        			// 如果登录的是团队经理，则查询该团队下的所有项目经理的报工情况
         			user1.setProGroupNo(proGroupNo);
         			List<BsUserInfor> bsUserInfor = userInforService.selectTeamManager(user1);
         			if(bsUserInfor.size()==0) {
         				// 如果该团队下没有项目经理,则查询所有人的报工情况
         				RpUserReportBean record = new RpUserReportBean();
         	 	 		record.setRouid(rouid);
         	 	 		record.setProTeamNo(proTeamNo);
         	 	 		record.setHappenDate(happenDate);
         	 	 		// 该团队经理下的所有人员的报工情况
         	 	 		List<RpUserReportBean> rsList = userNeedService.searchReviewJob(record);
         	 	 		
         	 	 		BsNeedInfor needInfor = new BsNeedInfor();
         	 	 		needInfor.setProTeamNo(proTeamNo);
         	 	 		needInfor.setRouid(rouid);
         	 	 		// 查询该团队经理下所有人员未审核和审核未通过的需求信息
         	 	 		List<BsNeedInfor> needInforList = userNeedService.searchNeedInforList(needInfor);
         	 	 		/*JSONArray json = new JSONArray();
	       	 	         for(BsNeedInfor pLog : needInforList){
	       	 	             JSONObject jo = new JSONObject();
	       	 	             jo.put("rouid", pLog.getRouid());
	       	 	             jo.put("needNo", pLog.getNeedNo());
	       	 	             jo.put("needName", pLog.getNeedName());
	       	 	             json.add(jo);
	       	 	         }*/
        	 	         
        	 	 		request.setAttribute("needInforList", needInforList);
         	 	 		request.setAttribute("list", rsList);
         			}else {
         				RpUserReportBean record = new RpUserReportBean();
         	 	 		record.setRouid(rouid);
         	 	 	   //record.setProGroupNo(user1.getProGroupNo());
         	 	 		record.setProTeamNo(user1.getProTeamNo());
         	 	 		// 如果有项目经理,则查询该团队经理团队下的所有项目经理的报工情况
         	 	 		List<RpUserReportBean> rsList = userNeedService.searchTeamReviewJob(record);	
         	 	 		
         	 	 		BsNeedInfor needInfor = new BsNeedInfor();
         	 	 	   //needInfor.setProGroupNo(user1.getProGroupNo());
         	 	 		needInfor.setProTeamNo(user1.getProTeamNo());
         	 	 		needInfor.setRouid(rouid);
         	 	 		// 查询出该团队下的所有项目经理未审核和审核未通过的需求信息
         	 	 		List<BsNeedInfor> needInforList = userNeedService.searchTeamNeedInforList(needInfor);	
         	 	 		/*JSONArray json = new JSONArray();
	       	 	         for(BsNeedInfor pLog : needInforList){
	       	 	             JSONObject jo = new JSONObject();
	       	 	             jo.put("rouid", pLog.getRouid());
	       	 	             jo.put("needNo", pLog.getNeedNo());
	       	 	             jo.put("needName", pLog.getNeedName());
	       	 	             json.add(jo);
	       	 	         }*/
        	 	         
        	 	 		request.setAttribute("needInforList", needInforList);
         	 	 		request.setAttribute("list", rsList);
         			}
         		}else if("70".equals(positionType)) {
         			// 如果登录的是项目经理，则查询该项目组下的所有人员报工情况
         			
         			RpUserReportBean record = new RpUserReportBean();
         			record.setRouid(rouid);
         	 		record.setProTeamNo(proTeamNo);
         	 		record.setHappenDate(happenDate);
         	 		// 该项目经理下的所有人员的报工情况
         	 		List<RpUserReportBean> rsList = userNeedService.searchReviewJob(record);
         	 		
         	 		BsNeedInfor needInfor = new BsNeedInfor();
         	 		needInfor.setProTeamNo(proTeamNo);
         	 		needInfor.setRouid(rouid);
         	 		// 查询该项目经理下所有人员未审核和审核未通过的需求信息
         	 		List<BsNeedInfor> needInforList = userNeedService.searchNeedInforList(needInfor);
         	 		/*JSONArray json = new JSONArray();
	   	 	         for(BsNeedInfor pLog : needInforList){
	   	 	             JSONObject jo = new JSONObject();
	   	 	             jo.put("rouid", pLog.getRouid());
	   	 	             jo.put("needNo", pLog.getNeedNo());
	   	 	             jo.put("needName", pLog.getNeedName());
	   	 	             json.add(jo);
	   	 	         }*/
    	 	         
    	 	 		request.setAttribute("needInforList", needInforList);
         	 		request.setAttribute("list", rsList);
         		}
        		request.setAttribute("positionType", positionType);
        		request.setAttribute("proTeamNo", proTeamNo);
         		request.setAttribute("proGroupNo", proGroupNo);
        		request.setAttribute("rouid", rouid);
        		request.setAttribute("happenDate", happenDate);
        		//request.setAttribute("choseName", "");
        		return "frame/html/job/reviewJob";
            }
        } else {
            request.setAttribute("wechatBackJson", "空");
            try {
                String wechatUrl = wechatService.buildAuthorizeUrl(JAppConfig.APP_URL + "/page/job/searchReviewJob1.do?happenDate="+happenDate, JAppConfig.APP_URL + "/page/job/searchReviewJob1.do?happenDate="+happenDate);
                request.setAttribute("wechatUrl", wechatUrl);
            } catch (IOException e) {
                logger.error("buildAuthorizeUrl e:" + e.getMessage());
                e.printStackTrace();
            }
        }
        return "frame/html/user/login";
	}
	
	/**
	 * 查询项目经理管理下所有人员的未审核/审核未通过的报工记录
	 */
	@RequestMapping("/page/job/searchReviewJob.do")
	public String searchReviewJob( HttpServletRequest request,HttpServletResponse response, String proGroupNo, String proTeamNo, String rouid, String happenDate, String needId, String positionType, String choseName ) throws JTransException, JSystemException{
		BsUserInfor user1 = userInforService.selectUserInforByUserNo(rouid);
		if("69".equals(positionType)) {
			// 如果登录的是团队经理，则查询该团队下的所有项目经理的报工情况
 			user1.setProGroupNo(proGroupNo);
 			List<BsUserInfor> bsUserInfor = userInforService.selectTeamManager(user1);
 			if(bsUserInfor.size()==0) {
 				// 如果该团队下没有项目经理,则查询所有人的报工情况
 				RpUserReportBean record = new RpUserReportBean();
 	 	 		record.setRouid(rouid);
 	 	 		record.setProTeamNo(proTeamNo);
 	 	 		record.setHappenDate(happenDate);
 	 	 		record.setNeedId(needId);
 	 	 		// 该项目经理下的所有人员的报工情况
 	 	 		List<RpUserReportBean> rsList = userNeedService.searchReviewJob(record);
 	 	 		
 	 	 		BsNeedInfor needInfor = new BsNeedInfor();
 	 	 		needInfor.setProTeamNo(proTeamNo);
 	 	 		needInfor.setRouid(rouid);
 	 	 		// 查询该项目经理下所有人员未审核和审核未通过的需求信息
 	 	 		List<BsNeedInfor> needInforList = userNeedService.searchNeedInforList(needInfor);
 	 	 		/*JSONArray json = new JSONArray();
	 	         for(BsNeedInfor pLog : needInforList){
	 	             JSONObject jo = new JSONObject();
	 	             jo.put("rouid", pLog.getRouid());
	 	             jo.put("needNo", pLog.getNeedNo());
	 	             jo.put("needName", pLog.getNeedName());
	 	             json.add(jo);
	 	         }*/
	 	         
	 	 		request.setAttribute("needInforList", needInforList);
 	 	 		request.setAttribute("list", rsList);
 			}else {
 				RpUserReportBean record = new RpUserReportBean();
 	 	 		record.setRouid(rouid);
 	 	 	   //record.setProGroupNo(user1.getProGroupNo());
 	 	 		record.setProTeamNo(user1.getProTeamNo());
 	 	 		// 如果有项目经理,则查询该团队经理团队下的所有项目经理的报工情况
 	 	 		List<RpUserReportBean> rsList = userNeedService.searchTeamReviewJob(record);	
 	 	 		
 	 	 		BsNeedInfor needInfor = new BsNeedInfor();
 	 	 	    //needInfor.setProGroupNo(user1.getProGroupNo());
 	 	 		needInfor.setProTeamNo(user1.getProTeamNo());
 	 	 		needInfor.setRouid(rouid);
 	 	 		// 查询出该团队下的所有项目经理未审核和审核未通过的需求信息
 	 	 		List<BsNeedInfor> needInforList = userNeedService.searchTeamNeedInforList(needInfor);	
 	 	 		/*JSONArray json = new JSONArray();
 		         for(BsNeedInfor pLog : needInforList){
 		             JSONObject jo = new JSONObject();
 		             jo.put("rouid", pLog.getRouid());
 		             jo.put("needNo", pLog.getNeedNo());
 		             jo.put("needName", pLog.getNeedName());
 		             json.add(jo);
 		         }*/
 	 	         
 	 	 		request.setAttribute("needInforList", needInforList);
 	 	 		request.setAttribute("list", rsList);
 			}
 			
 		}else if("70".equals(positionType)) {
 			// 如果登录的是项目经理，则查询该项目组下的所有人员报工情况
 			
 			RpUserReportBean record = new RpUserReportBean();
 			record.setRouid(rouid);
 	 		record.setProTeamNo(proTeamNo);
 	 		record.setHappenDate(happenDate);
 	 		record.setNeedId(needId);
 	 		// 该项目经理下的所有人员的报工情况
 	 		List<RpUserReportBean> rsList = userNeedService.searchReviewJob(record);
 	 		
 	 		BsNeedInfor needInfor = new BsNeedInfor();
 	 		needInfor.setProTeamNo(proTeamNo);
 	 		needInfor.setRouid(rouid);
 	 		// 查询该项目经理下所有人员未审核和审核未通过的需求信息
 	 		List<BsNeedInfor> needInforList = userNeedService.searchNeedInforList(needInfor);
 	 		/*JSONArray json = new JSONArray();
	         for(BsNeedInfor pLog : needInforList){
	             JSONObject jo = new JSONObject();
	             jo.put("rouid", pLog.getRouid());
	             jo.put("needNo", pLog.getNeedNo());
	             jo.put("needName", pLog.getNeedName());
	             json.add(jo);
	         }*/
 	         
 	 		request.setAttribute("needInforList", needInforList);
 	 		request.setAttribute("list", rsList);
 		}
		request.setAttribute("wechatUrl", "0");
		request.setAttribute("positionType", positionType);
		request.setAttribute("proTeamNo", proTeamNo);
 		request.setAttribute("proGroupNo", proGroupNo);
		request.setAttribute("rouid", rouid);
		request.setAttribute("happenDate", happenDate);
		request.setAttribute("needId", needId);
		//request.setAttribute("choseName", choseName);
 		return "frame/html/job/reviewJob";
	}
	
	/**
	 * 审核报工
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/job/confirmJob.do")
	public void confirmJob( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		 JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
		 BsUserNeedRel req = context.obtainReqModel(BsUserNeedRel.class);
		 userNeedService.updateReportStateByRouId(req);
		 String rouid = req.getRouid();
		/* if("01".equals(req.getReportState())) {
			 batchManagerCtrl.confirmSendWxMsg(rouid);
		 }else*/ if("02".equals(req.getReportState())) {
			 batchManagerCtrl.unConfirmSendWxMsg(rouid);
		 }
	     Map rspModel = new HashMap();
	     context.buildAppResponse(rspModel);
	}
	
	/**
	 * 批量审核报工
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/job/passAll.do")
	public void passAll( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		 JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
		 BsUserNeedRel req = context.obtainReqModel(BsUserNeedRel.class);
		 String checkedRouId = req.getRouid();
		 String[] rouIdList = checkedRouId.split(",");
		 for(int i=0;i<rouIdList.length;i++) {
			 req.setRouid(rouIdList[i]);
			 userNeedService.updateReportStateByRouId(req);
			/* if("01".equals(req.getReportState())) {
				 batchManagerCtrl.confirmSendWxMsg(rouIdList[i]);
			 }else*/ if("02".equals(req.getReportState())) {
				 batchManagerCtrl.unConfirmSendWxMsg(rouIdList[i]);
			 }
		 }
	     Map rspModel = new HashMap();
	     context.buildAppResponse(rspModel);
	}
}
